﻿using UnityEngine;
using System.Collections;

public class OptionsScreenScript : MonoBehaviour {

	GameObject metroidSprite;
	Animator spriteAnim;
	TextMesh highScoreText;
	//TextMesh lowerText;
	float crazyTime = 0.5f;
	float crazyTimeStart = 0.5f;
	bool getCrazy = false;
	int clickCount = 0;
	bool canClick = true;
	
	Animator backAnim;
	SpriteRenderer backRend;

	SpriteRenderer slideDifficulty;
	SpriteRenderer slideSound;
	SpriteRenderer slideMusic;

	SpriteRenderer slideBarDifficulty;
	SpriteRenderer slideBarSound;
	SpriteRenderer slideBarMusic;

	AudioSource musicSource;
	int soundSetting;
	int musicSetting;
	int difficultySetting;

	TextMesh difficultyText;
	TextMesh soundText;
	TextMesh musicText;

	TextMesh debugText;
	
	// Use this for initialization
	void Start () {
		this.backAnim = (Animator)GameObject.Find ("Back").GetComponent (typeof(Animator));
		this.backRend = (SpriteRenderer)GameObject.Find ("Back").GetComponent (typeof(SpriteRenderer));

		this.backAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.BackUp);

		this.slideDifficulty = (SpriteRenderer)GameObject.Find ("SliderMarker_Difficulty").GetComponent (typeof(SpriteRenderer));
		this.slideSound = (SpriteRenderer)GameObject.Find ("SliderMarker_Sound").GetComponent (typeof(SpriteRenderer));
		this.slideMusic = (SpriteRenderer)GameObject.Find ("SliderMarker_Music").GetComponent (typeof(SpriteRenderer));

		this.slideBarDifficulty = (SpriteRenderer)GameObject.Find ("SliderHorizontal_Difficulty").GetComponent (typeof(SpriteRenderer));
		this.slideBarSound = (SpriteRenderer)GameObject.Find ("SliderHorizontal_Sound").GetComponent (typeof(SpriteRenderer));
		this.slideBarMusic = (SpriteRenderer)GameObject.Find ("SliderHorizontal_Music").GetComponent (typeof(SpriteRenderer));

		this.musicSource = (AudioSource)GameObject.Find ("MainMusic").GetComponent (typeof(AudioSource));

		if (PlayerPrefs.HasKey ("MusicVolume")) {
			musicSetting = PlayerPrefs.GetInt ("MusicVolume");
		} else {
			musicSetting = 10;
			PlayerPrefs.SetInt("MusicVolume",musicSetting);
		}

		if (PlayerPrefs.HasKey ("SoundVolume")) {
			soundSetting = PlayerPrefs.GetInt ("SoundVolume");
		} else {
			soundSetting = 10;
			PlayerPrefs.SetInt("SoundVolume",soundSetting);
		}

		if (PlayerPrefs.HasKey ("Difficulty")) {
			difficultySetting = PlayerPrefs.GetInt ("Difficulty");
		} else {
			difficultySetting = 1;
			PlayerPrefs.SetInt("Difficulty",difficultySetting);
		}

		this.difficultyText = (TextMesh)GameObject.Find ("Text_Difficulty").GetComponent (typeof(TextMesh));
		this.musicText = (TextMesh)GameObject.Find ("Text_Music").GetComponent (typeof(TextMesh));
		this.soundText = (TextMesh)GameObject.Find ("Text_Sound").GetComponent (typeof(TextMesh));

		GameMaintainerSingleton.Instance.SetInitialRendererPosition (slideDifficulty, slideBarDifficulty, 2, difficultySetting);
		GameMaintainerSingleton.Instance.SetInitialRendererPosition (slideSound, slideBarSound, 10, soundSetting);
		GameMaintainerSingleton.Instance.SetInitialRendererPosition (slideMusic, slideBarMusic, 10, musicSetting);

		this.SetDifficultyText ();
		this.SetMusicText ();
		this.SetSoundText ();

		this.musicSource.volume = Mathf.Lerp(0.0f,0.3f,(float)musicSetting / 10.0f);

		//GameMaintainerSingleton.Instance.ShowAds ();
	}
	


	void SetDifficultyText() {
		switch(difficultySetting) {
		case 0: this.difficultyText.text = "Difficulty - Easy: 7 Lives, Can Stomp Wasps"; break;
		case 1: this.difficultyText.text = "Difficulty - Normal: 7 Lives"; break;
		case 2: this.difficultyText.text = "Difficulty - Hard: 5 Lives"; break;
		default: this.difficultyText.text = "ERROR - " + difficultySetting; break;
		}
	}

	void SetSoundText() {
		switch(soundSetting) {
		case 0: this.soundText.text = "Sound Effect Volume: Muted"; break;
		case 1: this.soundText.text = "Sound Effect Volume: 10%"; break;
		case 2: this.soundText.text = "Sound Effect Volume: 20%"; break;
		case 3: this.soundText.text = "Sound Effect Volume: 30%"; break;
		case 4: this.soundText.text = "Sound Effect Volume: 40%"; break;
		case 5: this.soundText.text = "Sound Effect Volume: 50%"; break;
		case 6: this.soundText.text = "Sound Effect Volume: 60%"; break;
		case 7: this.soundText.text = "Sound Effect Volume: 70%"; break;
		case 8: this.soundText.text = "Sound Effect Volume: 80%"; break;
		case 9: this.soundText.text = "Sound Effect Volume: 90%"; break;
		case 10: this.soundText.text = "Sound Effect Volume: 100%"; break;
		default: this.soundText.text = "ERROR - " + soundSetting; break;
		} 
	}

	void SetMusicText() {
		switch(musicSetting) {
		case 0: this.musicText.text = "Music Volume: Muted"; break;
		case 1: this.musicText.text = "Music Volume: 10%"; break;
		case 2: this.musicText.text = "Music Volume: 20%"; break;
		case 3: this.musicText.text = "Music Volume: 30%"; break;
		case 4: this.musicText.text = "Music Volume: 40%"; break;
		case 5: this.musicText.text = "Music Volume: 50%"; break;
		case 6: this.musicText.text = "Music Volume: 60%"; break;
		case 7: this.musicText.text = "Music Volume: 70%"; break;
		case 8: this.musicText.text = "Music Volume: 80%"; break;
		case 9: this.musicText.text = "Music Volume: 90%"; break;
		case 10: this.musicText.text = "Music Volume: 100%"; break;
		default: this.musicText.text = "ERROR - " + musicSetting; break;
		}
	}

	// Update is called once per frame
	void Update () {

		bool inBack = false;
		bool inBackInitially = false;

		bool inDifficulty = false;
		bool inSound = false;
		bool inMusic = false;

		bool inDifficultyBar = false;
		bool inSoundBar = false;
		bool inMusicBar = false;

		bool inDifficultyBarInitially = false;
		bool inSoundBarInitially = false;
		bool inMusicBarInitially = false;

		if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {

			inDifficulty = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideDifficulty, GameMaintainerSingleton.Instance.ClickDownPos,10);
			inSound = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideSound, GameMaintainerSingleton.Instance.ClickDownPos,10);
			inMusic = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideMusic, GameMaintainerSingleton.Instance.ClickDownPos,10);

			inDifficultyBar = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideBarDifficulty, GameMaintainerSingleton.Instance.Pos,10);
			inSoundBar = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideBarSound, GameMaintainerSingleton.Instance.Pos,10);
			inMusicBar = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideBarMusic, GameMaintainerSingleton.Instance.Pos,10);
		}

		if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {
			GameMaintainerSingleton.SpriteBoundsSplitViewModel vm = new GameMaintainerSingleton.SpriteBoundsSplitViewModel();
			if (inDifficulty) {
				//inDifficultyBarInitially = this.IsInSpriteBounds(this.slideBarDifficulty, clickDownPos);
				vm = GameMaintainerSingleton.Instance.CheckBar(inDifficulty,inDifficultyBar,this.slideDifficulty, this.slideBarDifficulty, GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 2,40);

				if (vm.SegmentNumber != -1) {
					difficultySetting = vm.SegmentNumber;
					//Debug.Log(vm.ActualDistance+"/"+vm.RelativeDistance+"/"+vm.SegmentNumber);
					this.SetDifficultyText ();

					PlayerPrefs.SetInt("Difficulty",difficultySetting);
					//Debug.Log("Difficulty NEW:" + difficultySetting);
				}
			}

			if (inSound) {
				vm = GameMaintainerSingleton.Instance.CheckBar(inSound,inSoundBar,this.slideSound, this.slideBarSound, GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 10,40);


				if (vm.SegmentNumber != -1) {
					soundSetting = vm.SegmentNumber;
					this.SetSoundText ();

					PlayerPrefs.SetInt("SoundVolume",soundSetting);
					//Debug.Log("SOUND NEW:"+soundSetting);
				}
			}

			if (inMusic) {
				vm = GameMaintainerSingleton.Instance.CheckBar(inMusic,inMusicBar,this.slideMusic, this.slideBarMusic, GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 10,40);
				if (vm.SegmentNumber != -1) {
					musicSetting = vm.SegmentNumber;
					this.SetMusicText();

					PlayerPrefs.SetInt("MusicVolume",musicSetting);
					//Debug.Log("MUSIC NEW:"+musicSetting);

					this.musicSource.volume = Mathf.Lerp(0.0f,0.3f,(float)musicSetting / 10.0f);
				}
			}
		}

		inBack = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.Pos);
		inBackInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.ClickDownPos);
		if (GameMaintainerSingleton.Instance.CheckButton(
			inBack,inBackInitially,
			this.backRend,this.backAnim,
			MenuButtonConstants.MenuConstants.BackDown,MenuButtonConstants.MenuConstants.BackUp,
			MenuButtonConstants.MenuTriggerString)) {

			//GameMaintainerSingleton.Instance.ShowAds();
			Application.LoadLevel(0);
		}
	}
}
